/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package accessor;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.Model;

/**
 *
 * @author Thanhnd
 */
public class ProductsAccessor extends Model {

    public int getMaxID() {
        int retVal = 0;
        ResultSet rs = null;
        Connection conn = null;
        CallableStatement callable = null;
        try {
            conn = getConnection();
            callable = conn.prepareCall("{ call procProducts_getMaxID() }");
            rs = callable.executeQuery();
            while (rs.next()) {
                retVal = rs.getInt("ID");
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ProductsAccessor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(ProductsAccessor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(ProductsAccessor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(ProductsAccessor.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                rs.close();
                callable.close();
                conn.close();
            } catch (SQLException ex) {
                Logger.getLogger(ProductsAccessor.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return retVal;
    }

    public int insertProducts(Products products) {
        int retVal = 0;
        Connection conn = null;
        CallableStatement callable = null;
        Model model = new Model();
        try {
            conn = getConnection();
            callable = conn.prepareCall("{ call procProducts_add(?,?,?,?,?,?,?) }");
            callable.setString(1, products.getProductName());
            callable.setInt(2, products.getDefaultCategoryID());
            callable.setInt(3, products.getQuantity());
            callable.setString(4, products.getDefaultImage());
            callable.setDouble(5, products.getPrice());
            callable.setString(6, products.getDateUpload());
            callable.setString(7, products.getLastUpdate());
            retVal = callable.executeUpdate();
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ProductsAccessor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(ProductsAccessor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(ProductsAccessor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(ProductsAccessor.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                callable.close();
                conn.close();
            } catch (SQLException ex) {
                Logger.getLogger(ProductsAccessor.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return retVal;
    }

    public int updateProducts(Products products) {
        int retVal = 0;
        Connection conn = null;
        CallableStatement callable = null;
        try {
            conn = getConnection();
            callable = conn.prepareCall("{ call procProducts_update(?,?,?,?,?,?,?) }");
            callable.setInt(1, products.getProductID());
            callable.setString(2, products.getProductName());
            callable.setInt(3, products.getDefaultCategoryID());
            callable.setInt(4, products.getQuantity());
            callable.setString(5, products.getDefaultImage());
            callable.setDouble(6, products.getPrice());
            callable.setString(7, products.getLastUpdate());
            retVal = callable.executeUpdate();
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ProductsAccessor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(ProductsAccessor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(ProductsAccessor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(ProductsAccessor.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                callable.close();
                conn.close();
            } catch (SQLException ex) {
                Logger.getLogger(ProductsAccessor.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return retVal;
    }

    public int deleteProducts(Products products) {
        int retVal = 0;
        Connection conn = null;
        CallableStatement callable = null;
        try {
            conn = getConnection();
            callable = conn.prepareCall("{ call procProducts_delete(?) }");
            callable.setInt(1, products.getProductID());
            retVal = callable.executeUpdate();
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ProductsAccessor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(ProductsAccessor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(ProductsAccessor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(ProductsAccessor.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                callable.close();
                conn.close();
            } catch (SQLException ex) {
                Logger.getLogger(ProductsAccessor.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return retVal;
    }

    public List<Products> getPagedProducts(String where, String orderBy, int pageIndex, int pageSize) {
        List<Products> list = new ArrayList<Products>();
        ResultSet rs = null;
        Connection conn = null;
        CallableStatement callable = null;
        try {
            conn = getConnection();
            callable = conn.prepareCall("{ call procProducts_getpaged(?,?,?,?) }");
            callable.setString(1, "");
            callable.setString(2, "");
            callable.setInt(3, pageIndex);
            callable.setInt(4, pageSize);
            rs = callable.executeQuery();
            while (rs.next()) {
                Products products = new Products();
                products.setProductID(rs.getInt(1));
                products.setProductName(rs.getString(2));
                products.setDefaultCategoryID(rs.getInt(3));
                products.setQuantity(rs.getInt(4));
                products.setDefaultImage(rs.getString(5));
                products.setPrice(rs.getDouble(6));
                products.setDateUpload(rs.getString(7));
                products.setLastUpdate(rs.getString(8));
                list.add(products);
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ProductsAccessor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(ProductsAccessor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(ProductsAccessor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(ProductsAccessor.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                rs.close();
                callable.close();
                conn.close();
            } catch (SQLException ex) {
                Logger.getLogger(ProductsAccessor.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return list;
    }

    public Products getProductById(int id) {
        Products products = new Products();
        ResultSet rs = null;
        Connection conn = null;
        CallableStatement callable = null;
        try {
            conn = getConnection();
            callable = conn.prepareCall("{ call procProducts_getOne(?) }");
            callable.setInt(1, id);
            rs = callable.executeQuery();
            while (rs.next()) {
                products.setProductID(rs.getInt(1));
                products.setProductName(rs.getString(2));
                products.setDefaultCategoryID(rs.getInt(3));
                products.setQuantity(rs.getInt(4));
                products.setDefaultImage(rs.getString(5));
                products.setPrice(rs.getDouble(6));
                products.setDateUpload(rs.getString(7));
                products.setLastUpdate(rs.getString(8));
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ProductsAccessor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(ProductsAccessor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(ProductsAccessor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(ProductsAccessor.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                rs.close();
                callable.close();
                conn.close();
            } catch (SQLException ex) {
                Logger.getLogger(ProductsAccessor.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return products;
    }

    public List<Products> getAllProducts() {
        List<Products> list = new ArrayList<Products>();
        ResultSet rs = null;
        Connection conn = null;
        CallableStatement callable = null;
        try {
            conn = getConnection();
            callable = conn.prepareCall("{ call procProducts_getall() }");
            rs = callable.executeQuery();
            while (rs.next()) {
                Products products = new Products();
                products.setProductID(rs.getInt(1));
                products.setProductName(rs.getString(2));
                products.setDefaultCategoryID(rs.getInt(3));
                products.setQuantity(rs.getInt(4));
                products.setDefaultImage(rs.getString(5));
                products.setPrice(rs.getDouble(6));
                products.setDateUpload(rs.getString(7));
                products.setLastUpdate(rs.getString(8));
                list.add(products);
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ProductsAccessor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(ProductsAccessor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(ProductsAccessor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(ProductsAccessor.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                rs.close();
                callable.close();
                conn.close();
            } catch (SQLException ex) {
                Logger.getLogger(ProductsAccessor.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return list;
    }
}
